package com.nuo.backend.modules.security.controller;

import cn.hutool.core.lang.UUID;
import com.google.code.kaptcha.Producer;
import com.nuo.backend.common.utils.R;
import com.nuo.backend.modules.security.po.LoginPO;
import com.nuo.backend.modules.security.po.RegisterPO;
import com.nuo.backend.modules.security.po.JwtPO;
import com.nuo.backend.modules.security.service.SecCaptchaService;
import com.nuo.backend.modules.security.service.SecUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @description: TODO 登陆
 * @author nuo
 * @date 2022/12/4 13:48
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("sec")
public class SecLoginController {

    @Autowired
    private SecUserService secUserService;


    @Autowired
    private SecCaptchaService secCaptchaService;

    /**
     * TODO 获取 uuid
     */
    @RequestMapping("/uuid")
    public R uuid()throws IOException {
        return R.ok().put("uuid", UUID.randomUUID().toString());
    }

    /**
     * TODO 获取验证码图片
     */
    @Transactional(rollbackFor = Exception.class)
    @GetMapping("/captcha.jpg")
    public void captchaImg(HttpServletRequest request, HttpServletResponse response, String uuid)throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        BufferedImage image = secCaptchaService.getCaptchaImg(request, uuid);

        // // 单选、多选
        // [{
        //     order: 选项的顺序1
        //     option: 选项内容1
        // },{
        //     order: 选项的顺序2
        //     option: 选项内容2
        // }]
        //
        // // 多项填空
        // [{
        //     order: 填空的顺序1
        //     describe: 填空说明1
        //     ans: 用户填的内容1
        // },{
        //     order: 填空的顺序2
        //     describe: 填空说明2
        //     ans: 用户填的内容2
        // }]

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }

    /**
     * TODO 获取 Base64 验证码图片
     */
    @Transactional(rollbackFor = Exception.class)
    @GetMapping("/captchaStr")
    public R captchaStr(HttpServletRequest request, String uuid) {
        return R.ok().put("base64Img", secCaptchaService.captchaStr(request, uuid));
    }


    @PostMapping("/login")
    public R login(@RequestBody LoginPO po) {
        return R.ok();
    }

    @PostMapping("/register")
    public R register(@RequestBody RegisterPO po) {
        return secUserService.register(po);
    }

    @PostMapping("/getUserBySession")
    public R getUserBySession(HttpServletRequest request) {
        return secUserService.getUserBySession(request);
    }

    @PostMapping("/getUserByHeaderToken")
    public R getUserByHeaderToken(HttpServletRequest request) {
        return secUserService.getUserByHeaderToken(request);
    }

    @PostMapping("/getUserByBodyToken")
    public R getUserByBodyToken(@RequestBody JwtPO po) {
        return secUserService.getUserByBodyToken(po);
    }

    @RequestMapping("/getUserBySec")
    public R getUserBySec() {
        return secUserService.getUserBySec();
    }

    @PostMapping("/logout")
    public R logout() {
        return R.ok();
    }

}
